﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelGenerator.SpreadSheet.Styles;

namespace ExcelGenerator.SpreadSheet
{
    /// <summary>
    /// Options available for cell data type
    /// </summary>
    public enum DataType
    {
        Number,
        String,
        DateTime,
        Boolean
    }

    public class Cell
    {
        
        private object _value;
        private string _style;
        private int _span;
        private DataType _type;

        /// <summary>
        /// Creates an empty new cell
        /// </summary>
        public Cell(){
            _span = 1;
            _type = DataType.String;
            _style = "Default";
        }

        /// <summary>
        /// Creates a new cell
        /// </summary>
        /// <param name="value">Value to this cell data</param>
        /// <param name="span">Specifies the number of adjacent cells across (right) from the current cell to merge.</param>
        /// <param name="style">Style to apply in this cell, if not set the "Default" style will be used</param>
        /// <param name="type">Defines cell data type.</param>
        public Cell(String value, int span, String style = "Default", DataType type = DataType.String)
        {
            _value = value;
            _style = style;
            _span = span;
            _type = type;
        }

        /// <summary>
        /// Creates a new cell
        /// </summary>
        /// <param name="value">Value to this cell data</param>
        /// <param name="span">Specifies the number of adjacent cells across (right) from the current cell to merge.</param>
        /// <param name="style">Style to apply in this cell, if not set the "Default" style will be used</param>
        public Cell(String value, int span, String style = "Default")
        {
            _value = value;
            _style = style;
            _span = span;
            _type = DataType.String;
        }

        /// <summary>
        /// Creates a new cell
        /// </summary>
        /// <param name="value">Value to this cell data</param>
        /// <param name="style">Style to apply in this cell, if not set the "Default" style will be used</param>
        public Cell(String value, String style = "Default")
        {
            if (!StylesManager.containsStyle(style))
            {
                throw new ArgumentException("Trying to create a cell with a style not defined", "style");
            }

            _value = value;
            _style = style;
            _span = 1;
            _type = DataType.String;
        }

        /// <summary>
        /// Creates a new cell
        /// </summary>
        /// <param name="value">Value to this cell data</param>
        /// <param name="type">Defines cell data type.</param>
        /// <param name="style">Style to apply in this cell, if not set the "Default" style will be used</param>
        public Cell(String value, DataType type, String style = "Default")
        {
            _value = value;
            _style = style;
            _span = span;
            _type = type;
        }

        /// <summary>
        /// Value to this cell data
        /// </summary>
        public object Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }

        /// <summary>
        /// Style to apply in this cell
        /// </summary>
        public string style
        {
            get
            {
                return _style;
            }
            set
            {
                if (!StylesManager.containsStyle(value))
                {
                    throw new ArgumentException("Trying to create a cell with a style not defined", "style");
                }
                else
                {
                    _style = value;
                }
            }
        }

        /// <summary>
        /// Specifies the number of adjacent cells across (right) from the current cell to merge.
        /// </summary>
        public int span
        {
            get
            {
                return (_span <= 0) ? 1 : _span;
            }
            set
            {
                if (value <= 0)
                {
                    _span = 1;
                }
                else
                {
                    _span = value;
                }    
            }
        }

        /// <summary>
        /// Specifies the type for cell data.
        /// </summary>
        public DataType type
        {
            get
            {
                return (_type == null) ? DataType.String : _type;
            }
            set
            {
                if (_type == null)
                {
                    _type = DataType.String;
                }
                else
                {
                    _type = value;
                }
                
            }
        }
        
    }
}
